const HOST = 'https://www.haihong.asia';

function get(url, data = {}, options = {}) {
	let user;
	try {
		user = uni.getStorageSync('userinfo');
		if (user) {
			console.warn(user);
		}
	} catch (e) {
		user = {};
	}
	
	return uni.request({
		url: `${HOST}${url}`,
		data: Object.assign(data, {
			uid: user.id,
		}),
		method: 'GET',
		timeout: 6000,
		dataType: 'json',
		...options,
		header: {
			'TOKEN': user.token,
		},
	}).then((res) => {
		const [, { data: { code , data, msg } } ] = res;
		if (code === 1) {
			return data;
		}
		return Promise.reject(msg);
	});
}

function post(url, data = {}, options = {}) {
	let user;
	try {
		user = uni.getStorageSync('userinfo');
		if (user) {
			console.warn(user);
		}
	} catch (e) {
		user = {};
	}
	return uni.request({
		url: `${HOST}${url}`,
		data: Object.assign(data, {
			uid: user.id,
		}),
		method: 'POST',
		timeout: 6000,
		dataType: 'json',
		...options,
		header: {
			'TOKEN': user.token,
		},
	}).then((res) => {
		const [, { data: { code , data, msg } } ] = res;
		if (code === 1) {
			return data;
		}
		return Promise.reject(msg);
	});
}

export {
	get,
	post,
}
